package com.atguigu.gulimall.member.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberLevelDao;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.service.MemberLevelService;


@Service("memberLevelService")
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelDao, MemberLevelEntity> implements MemberLevelService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberLevelEntity> page = this.page(
                new Query<MemberLevelEntity>().getPage(params),
                new QueryWrapper<MemberLevelEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<MemberLevelEntity> memberLevelEntityQueryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            memberLevelEntityQueryWrapper.and((wrapper)->{
                wrapper.eq("id",key).or().like("name",key);
            });
        }

        String gmin = (String) params.get("gmin");
        if (!StringUtils.isEmpty(gmin)){
            memberLevelEntityQueryWrapper.ge("growth_point",gmin);
        }

        String gmax = (String) params.get("gmax");
        if (!StringUtils.isEmpty(gmax)){
            try {
                BigDecimal bigDecimal = new BigDecimal(gmax);
                if (bigDecimal.compareTo(new BigDecimal("0"))==1){
                    memberLevelEntityQueryWrapper.le("growth_point",gmax);
                }

            }catch (Exception e) {
            }
        }
        String fmin = (String) params.get("fmin");
        if (!StringUtils.isEmpty(fmin)){
            memberLevelEntityQueryWrapper.ge("free_freight_point",fmin);
        }

        String fmax = (String) params.get("fmax");
        if (!StringUtils.isEmpty(fmax)){
            try {
                BigDecimal bigDecimal = new BigDecimal(fmax);
                if (bigDecimal.compareTo(new BigDecimal("0"))==1){
                    memberLevelEntityQueryWrapper.le("free_freight_point",fmax);
                }

            }catch (Exception e) {
            }
        }


        IPage<MemberLevelEntity> page = this.page(
                new Query<MemberLevelEntity>().getPage(params),
                memberLevelEntityQueryWrapper
        );

        return new PageUtils(page);
    }

}